<template>
  <div class="login-modal-overlay">
    <div class="login-modal-container" :class="{ 'right-panel-active': isRegisterMode }">
      <!-- 登录面板 -->
      <div class="form-container login-container">
        <h2 class="form-title">账号登录</h2>
        <div class="form-content">
          <div class="form-group">
            <input 
              type="text" 
              v-model="loginForm.username" 
              placeholder="用户名" 
              class="form-input"
            />
          </div>
          
          <div class="form-group">
            <input 
              type="password" 
              v-model="loginForm.password" 
              placeholder="密码" 
              class="form-input"
            />
          </div>
          
          <div class="forgot-password">
            <a href="#" class="forgot-link">忘记密码?</a>
          </div>
          
          <button 
            class="form-button" 
            @click="handleLogin" 
            :disabled="loading"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </button>
        </div>
      </div>
      
      <!-- 注册面板 -->
      <div class="form-container register-container">
        <h2 class="form-title">创建账号</h2>
        <div class="form-content">
          <div class="form-group">
            <input 
              type="text" 
              v-model="registerForm.username" 
              placeholder="用户名" 
              class="form-input"
            />
          </div>
          
          <div class="form-group">
            <input 
              type="email" 
              v-model="registerForm.email" 
              placeholder="邮箱" 
              class="form-input"
            />
          </div>
          
          <div class="form-group">
            <input 
              type="password" 
              v-model="registerForm.password" 
              placeholder="密码" 
              class="form-input"
            />
          </div>
          
          <button 
            class="form-button" 
            @click="handleRegister" 
            :disabled="loading"
          >
            {{ loading ? '注册中...' : '注 册' }}
          </button>
        </div>
      </div>
      
      <!-- 覆盖面板 -->
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <h2 class="overlay-title">欢迎回来!</h2>
            <p class="overlay-text">请使用您的账号信息登录，继续访问平台</p>
            <button class="overlay-button ghost" @click="toggleMode">登 录</button>
          </div>
          <div class="overlay-panel overlay-right">
            <h2 class="overlay-title">没有账号?</h2>
            <p class="overlay-text">立即注册账号，开始您的全新体验！</p>
            <button class="overlay-button ghost" @click="toggleMode">注 册</button>
          </div>
        </div>
      </div>
      
      <!-- 关闭按钮 -->
      <button class="close-button" @click="close">
        <span>×</span>
      </button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import UserUtil from '@/utils/User/User.js';
import { ElMessage } from 'element-plus';

const emit = defineEmits(["clickChild"]);
const router = useRouter();
const loading = ref(false);
const isRegisterMode = ref(false);

// 登录表单数据
const loginForm = ref({
  username: '',
  password: ''
});

// 注册表单数据
const registerForm = ref({
  username: '',
  email: '',
  password: ''
});

// 切换登录/注册模式
const toggleMode = () => {
  isRegisterMode.value = !isRegisterMode.value;
};

// 处理登录
const handleLogin = async () => {
  if (!loginForm.value.username || !loginForm.value.password) {
    ElMessage.warning('请填写用户名和密码');
    return;
  }

  loading.value = true;
  try {
    const result = await UserUtil.login(loginForm.value.username, loginForm.value.password);
    if (result) {
      ElMessage.success('登录成功');
      close();
      router.push('/dashboard');
    } else {
      ElMessage.error('登录失败，请检查用户名和密码');
    }
  } catch (error) {
    ElMessage.error('登录过程中发生错误');
    console.error('登录错误:', error);
  } finally {
    loading.value = false;
  }
};

// 处理注册
const handleRegister = async () => {
  if (!registerForm.value.username || !registerForm.value.email || !registerForm.value.password) {
    ElMessage.warning('请填写完整的注册信息');
    return;
  }

  loading.value = true;
  try {
    const result = await UserUtil.register(
      registerForm.value.username,
      registerForm.value.password,
      registerForm.value.email
    );
    
    if (result) {
      ElMessage.success('注册成功，请登录');
      isRegisterMode.value = false; // 切换回登录面板
    } else {
      ElMessage.error('注册失败，请稍后再试');
    }
  } catch (error) {
    ElMessage.error('注册过程中发生错误');
    console.error('注册错误:', error);
  } finally {
    loading.value = false;
  }
};

// 关闭弹窗
const close = () => {
  emit("clickChild", false);
};
</script>

<style lang="less" scoped>
.login-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.login-modal-container {
  position: relative;
  width: 768px;
  height: 480px;
  background-color: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  display: flex;
  min-height: 480px;
}

.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
  display: flex;
  flex-direction: column;
  padding: 0 50px;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.login-container {
  left: 0;
  width: 50%;
  z-index: 2;
}

.register-container {
  left: 0;
  width: 50%;
  opacity: 0;
  z-index: 1;
}

.login-modal-container.right-panel-active .login-container {
  transform: translateX(100%);
  opacity: 0;
}

.login-modal-container.right-panel-active .register-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 5;
  animation: show 0.6s;
}

@keyframes show {
  0%, 49.99% {
    opacity: 0;
    z-index: 1;
  }
  
  50%, 100% {
    opacity: 1;
    z-index: 5;
  }
}

.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}

.login-modal-container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}

.overlay {
  background: linear-gradient(to right, #ff416c, #ff4b2b);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 0 0;
  color: #FFFFFF;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.login-modal-container.right-panel-active .overlay {
  transform: translateX(50%);
}

.overlay-panel {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 40px;
  text-align: center;
  top: 0;
  height: 100%;
  width: 50%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-left {
  transform: translateX(-20%);
}

.login-modal-container.right-panel-active .overlay-left {
  transform: translateX(0);
}

.overlay-right {
  right: 0;
  transform: translateX(0);
}

.login-modal-container.right-panel-active .overlay-right {
  transform: translateX(20%);
}

.form-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 30px;
  color: #333;
}

.form-content {
  width: 100%;
  max-width: 300px;
}

.form-group {
  margin-bottom: 20px;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #eee;
  border-radius: 5px;
  font-size: 14px;
  background-color: #f8f8f8;
  transition: border-color 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #ff4b2b;
  background-color: #fff;
}

.forgot-password {
  text-align: right;
  margin-bottom: 20px;
}

.forgot-link {
  color: #666;
  font-size: 13px;
  text-decoration: none;
  transition: color 0.3s;
}

.forgot-link:hover {
  color: #ff4b2b;
}

.form-button {
  width: 100%;
  padding: 12px 15px;
  background-color: #ff4b2b;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s;
}

.form-button:hover {
  background-color: #e63e20;
}

.form-button:disabled {
  background-color: #ffaa9d;
  cursor: not-allowed;
}

.overlay-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 20px;
}

.overlay-text {
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 30px;
  opacity: 0.9;
}

.overlay-button {
  padding: 12px 45px;
  background-color: transparent;
  color: white;
  border: 1px solid white;
  border-radius: 5px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.overlay-button:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.ghost {
  background-color: transparent;
}

.close-button {
  position: absolute;
  top: 15px;
  right: 15px;
  background: none;
  border: none;
  color: #999;
  font-size: 24px;
  cursor: pointer;
  z-index: 1000;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s;
}

.close-button:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: #333;
}
</style>
